package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Category;
import com.itheima.entity.Setmeal;
import com.itheima.entity.SetmealDish;
import com.itheima.service.CatergoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @email 1572178030@qq.com
 * @author:黄海宾
 * @date: 2023/2/10  11:04
 */

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CatergoryService catergoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.SetmealWithDish(setmealDto);
        return R.success("保存套餐成功！！");
    }

    @GetMapping("/page")
    public R<Page> queryPage(int page, int pageSize, String name) {
        //把Setmeal中的数据显示到前端
        Page pageBean = new Page(page, pageSize);

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name);
        setmealService.page(pageBean, queryWrapper);
        //要把套餐中的套餐分类名字显示到前端

        Page pageBeanCopy = new Page();
        BeanUtils.copyProperties(pageBean, pageBeanCopy, "records");

        //循环的到每个records并给他们找到分类中的名字
        List<Setmeal> records = pageBean.getRecords();

        List<SetmealDto> recordsList = records.stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);

            Long categoryId = item.getCategoryId();
            Category category = catergoryService.getById(categoryId);

            setmealDto.setCategoryName(category.getName());

            return setmealDto;

        }).collect(Collectors.toList());

        log.info("recordsList{}", recordsList);
        pageBeanCopy.setRecords(recordsList);
        return R.success(pageBeanCopy);

    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {


        List<Setmeal> setmeals = setmealService.listByIds(ids);
        for (Setmeal setmeal : setmeals) {
            if (setmeal.getStatus() == 1) {
                return R.error("该商品已启用，不能被删除");
            }
        }
  /*      setmeals.stream().map(item->{
            if (item.getStatus()==1){
                return R.error("该商品已启用，不能被删除");
            }
           return false;
        });*/
        //删除setmeal 中的数据
        setmealService.removeByIds(ids);
        //删除setmealDish中的数据
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(queryWrapper);

        return R.success("套餐删除成功");

    }

    //：起售/停售套餐
    @PostMapping("/status/{type}")
    public R<String> saleStatus(@RequestParam List<Long> ids, @PathVariable int type) {
        List<Setmeal> setmeals = setmealService.listByIds(ids);
        for (Setmeal setmeal : setmeals) {
            //不用判断是否相同，相同就不跟新，不同就跟新
       /*     if (!(setmeal.getStatus() == type)) {
                setmeal.setStatus(type);
                setmealService.updateById(setmeal);
                return R.success("操作成功");
            }*/

            setmeal.setStatus(type);
            setmealService.updateById(setmeal);
            return R.success("操作成功");
        }
        return R.error("操作失败");
    }

    //修改套餐
    //根据套餐id查询套餐数据
    @GetMapping("/{id}")
    public R<SetmealDto> updateQuery(@PathVariable Long id) {
        //全部放到该类中
        SetmealDto setmealDto = new SetmealDto();
        //根据id查到Setmeal数据
        Setmeal setmeal = setmealService.getById(id);

        BeanUtils.copyProperties(setmeal, setmealDto);
        //查到setmealDish的数据
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateWithSetmeal(setmealDto);
        return R.success("修改套餐成功");
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
/*    @GetMapping("/list")
    public R<List<Setmeal>> getBySetmeal(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(*//*categoryId != null,*//* Setmeal::getCategoryId, categoryId);
        queryWrapper.eq(*//*status != null,*//* Setmeal::getStatus, status);

        *//*queryWrapper.orderByDesc(Setmeal::getUpdateTime);*//*
        List<Setmeal> setmealList = setmealService.list(queryWrapper);
        return R.success(setmealList);
    }
}*/
    @GetMapping("/list")
    public R<List<Setmeal>> getBySetmeal1(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());

        /*queryWrapper.orderByDesc(Setmeal::getUpdateTime);*/
        List<Setmeal> setmealList = setmealService.list(queryWrapper);
        return R.success(setmealList);
    }
}